package com.bicon.botu.mall.base.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.base.common.BaseConstants;
import com.bicon.botu.mall.base.entity.LogisticsCompany;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 物流公司 前端控制器
 * </p>
 *
 * @author tandk
 * @since 2018-05-10
 */
@RestController
@RequestMapping("/V3.0.0/logistics-company")
public class LogisticsCompanyController extends BaseController {

    /**
     * 分页查询物流公司列表
     *
     * @param pageNo   当前页
     * @param pageSize 每页显示数
     * @return
     */
    @GetMapping("/list")
    public BaseResult list(@RequestParam(value = "pageNo") int pageNo,
                           @RequestParam(value = "pageSize") int pageSize) {
        Page<LogisticsCompany> page = new LogisticsCompany().selectPage(new Page<>(pageNo, pageSize), new EntityWrapper<LogisticsCompany>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("sort", false));
        return sendResult200(page);
    }

    /**
     * 查询启用的物流公司
     *
     * @return
     */
    @GetMapping("/query")
    public BaseResult query() {
        List<LogisticsCompany> list = new LogisticsCompany().selectList(new EntityWrapper<LogisticsCompany>().setSqlSelect("id, name").eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("sort", false));
        return sendResult200(list);
    }

    /**
     * 保存物流公司列表
     *
     * @param name    名称
     * @param website 网址
     * @param code    代码
     * @param phone   电话
     * @param sort    排序
     * @param status  状态（0关闭，1启用）
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestParam(value = "name") String name,
                          @RequestParam(value = "website", required = false) String website,
                          @RequestParam(value = "code", required = false) String code,
                          @RequestParam(value = "phone") String phone,
                          @RequestParam(value = "sort", required = false, defaultValue = "0") Integer sort,
                          @RequestParam(value = "status") int status) {
        BaseResult x = validateParams(name, phone, status);
        if (x != null) {
            return x;
        }
        //检查重复
        LogisticsCompany logisticsCompany = new LogisticsCompany().selectOne(new EntityWrapper<LogisticsCompany>().eq("name", name)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (logisticsCompany != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        logisticsCompany = new LogisticsCompany(null, name, website, code, phone, sort, status);
        logisticsCompany.preInsert();
        logisticsCompany.insert();
        return sendResult200();
    }

    /**
     * 修改物流公司列表
     *
     * @param id      主键
     * @param name    名称
     * @param website 网址
     * @param code    代码
     * @param phone   电话
     * @param sort    排序
     * @param status  状态（0关闭，1启用）
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(@RequestParam(value = "id") String id,
                          @RequestParam(value = "name") String name,
                          @RequestParam(value = "website", required = false) String website,
                          @RequestParam(value = "code", required = false) String code,
                          @RequestParam(value = "phone") String phone,
                          @RequestParam(value = "sort", required = false) int sort,
                          @RequestParam(value = "status") int status) {
        BaseResult x = validateParams(name, phone, status);
        if (x != null) {
            return x;
        }
        //检查重复
        LogisticsCompany logisticsCompany = new LogisticsCompany().selectOne(new EntityWrapper<LogisticsCompany>().notIn("id",id).eq("name", name)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (logisticsCompany != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        logisticsCompany = new LogisticsCompany(id, name, website, code, phone, sort, status);
        logisticsCompany.preUpdate();
        logisticsCompany.updateById();
        return sendResult200();
    }

    /**
     * 校验参数
     *
     * @param name 名称
     * @param phone 电话
     * @param status 状态（0关闭，1启用）
     * @return
     */
    private BaseResult validateParams(String name, String phone, Integer status) {
        if (StringUtils.isAnyBlank(name, phone)) {
            return sendResult400("参数不完整");
        }
        if (status != 0 && status != 1) {
            return sendResult400("状态参数不正确");
        }
        return null;
    }

    /**
     * 启用
     *
     * @param id     主键
     * @param status 状态（0关闭，1启用）
     * @return
     */
    @PostMapping("/status")
    public BaseResult status(@RequestParam(value = "id") String id, @RequestParam(value = "status") Integer status) {
        LogisticsCompany logisticsCompany = new LogisticsCompany();
        logisticsCompany.setId(id);
        logisticsCompany.setStatus(status);
        logisticsCompany.setLogicDelete(BaseConstants.DEL_LOGIC_NORMAL);
        return logisticsCompany.updateById() ? sendResult200() : sendResult500();
    }

    /**
     * 查询物流公司详情
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/detail")
    public BaseResult detail(@RequestParam(value = "id") String id) {
        LogisticsCompany logisticsCompany = new LogisticsCompany();
        logisticsCompany.setId(id);
        return sendResult200(logisticsCompany.selectById());
    }

    /**
     * 删除物流公司
     *
     * @param id 主键
     * @return
     */
    @PostMapping("/delete")
    public BaseResult delete(@RequestParam(value = "id") String id) {
        LogisticsCompany logisticsCompany = new LogisticsCompany();
        logisticsCompany.setId(id);
        logisticsCompany.preDelete();
        return logisticsCompany.updateById() ? sendResult200() : sendResult500();
    }
}
